<TITLE>Modules -- /Project</TITLE>
<NEXTID 1>
<H1>Rules for straight ANSI C:</H1>
<H2>Assumptions</H2>Object oriented programming classes are not supported.
<H2>A program</H2>A program's executable code is made up of
<UL>
<LI>global variables, knowable to all code.
<LI>several modules of routines and corresponding private global variables,
some of the routines and/or variables may be exported to other modules.
<LI>a main routine, which starts the program and normally exports nothing.
</UL>
<H2>Source code files</H2>For maintenance, the source code is written as:
<UL>
<LI>one or more ".d" files containing definitions (no variables nor routines!)
A ".d" file never generates code. The set of ".d" files implements
a hierarchical tree of dependencies between the definitions.
<LI>one or more modules, each consisting of a ".h" file and a ".c" file.
A ".c" file always generates code. A ".h" file defines only what the
module exports to others, and never generates any code. Its definitions
may however depend on ".d" files.
<LI>one main routine file, which is ".c" and possibly has no ".h" file
(it normally exports nothing).
</UL>
<H2>Simulating Modules with include files</H2>We try to simulate modules with include files. As stated above, the
source of a module consists of:
<UL>
<LI>a ".h" file with the exported constants, variables and routine headers,
<LI>a ".c" file with the private constants and variables and the code
of all routines.
</UL>The ".h" file is included by other modules, which must see the variable
as extern during their compilation. The exporting module also needs
the declarations, but without the "extern" keyword. Therefore either
the declarations occur again inside the module's ".c" file or we find
a mechanism of re-using the ".h" file. Writing the declarations twice
means a maintenance problem when changes happen. <P>
The following mechanism is proposed for including ".h" files:
<UL>
<LI>a declaration of an exported variable is preceded by the word "PROVIDE".
<LI>every ".h" file uses
</UL>
<XMP>                           #include <Importation.d>
				  
</XMP>
in order to define "PROVIDE" to be either null or "extern" depending
on whether the word "IMPORT" is defined.
<UL>
<LI>If a variable is initialised,
then its initial value is given as 
</UL>
<XMP>                           INITIALLY(initial value);

</XMP>
(no equal sign!). Example for initialising exported integer i to 5:
</UL>
<XMP>                           PROVIDE int i INITIALLY(5);

</XMP>The file Importation.d contains:
<XMP>                      #undef PROVIDE
                      #ifdef IMPORT
                         #define PROVIDE extern
                         #define INITIALLY(s)
                      #else
                         #define PROVIDE
                         #define INITIALLY(s) = s
                         #endif
		
</XMP>Thus, in a module A which imports from a module B, the ".c" file starts
with:<XMP>                      #define IMPORT
                         #include "B.h"
                         ...
                         #undef IMPORT
								 
</XMP>But module B's ".c" file has the line
<XMP>                      #include "B.h"

</XMP>outside of "#define IMPORT" / "#undef IMPORT" brackets.<P>
The ".h" file of a module never imports from other modules (but it
can of course use #include for definitions).
<H2>Global variables</H2>Because they are instantiated as code, they are written as a module.<P>
Because there are only variables, the ".h" file contains all the declarations
and the ".c" file is empty apart from an include for generation of
the code. The ".c" file is simply:
<XMP>                      #include "Globals.h"
                      
</XMP>
<H2>The Main routine</H2>The main routine's ".c" file looks like:
<XMP>                  #include <... .h>
                      #include <... .h>
                      
                      #define IMPORT
                      #include "Definitions.d"
                      #include "Globals.h"
                      #include "A.h"
                      #include "B.h"
                      #include "... ".h""
                      #undef IMPORT
 
                      
                 void main() {
                      ...
                      ...
                      }
    
</XMP><P>
<A NAME=0 HREF=http://info.cern.ch/hypertext/WWW/People.html#Cailliau> RC</A></A>